package com.book.book.storage;

import com.book.book.entity.Novel;
import com.book.book.service.NovelSpider;
import com.book.common.utils.NovelSpiderFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/***
 * @Author Lin
 * @Date Create By 22:15 2017/10/8
 * @Description
 */
public abstract class AbstractNovelStorage implements Processor {
    protected Map<String, String> tasks = new TreeMap<>();

    protected SqlSessionFactory sqlSessionFactory;

    public AbstractNovelStorage() throws FileNotFoundException {
        URL url = AbstractNovelStorage.class.getClassLoader().getResource("com/book/book/storage/SqlMapConfig.xml");
        String filepath = url.getPath();
        //System.out.println(filepath);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(new FileInputStream(filepath));
    }

    @Override
    @Transactional
    public void process() {

        ExecutorService service = Executors.newFixedThreadPool(tasks.size());
        List<Future<String>> futures = new ArrayList<>(tasks.size());

        for (Map.Entry<String, String> entry : tasks.entrySet()) {
            String key = entry.getKey();
            final String value = entry.getValue();
            futures.add(service.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    NovelSpider spider = NovelSpiderFactory.getNovelSpider(value);
                    Iterator<List<Novel>> iterator = spider.iterator(value, 10);
                    while (iterator.hasNext()) {
                        System.err.println("开始抓取[" + key + "] 的 URL:" + spider.next());
                        List<Novel> novels = iterator.next();
                        //if (key.contains("玄")||key.contains("武")||key.contains("都")||
                        //    key.contains("历")||key.contains("侦")||key.contains("网")||
                        //    key.contains("科")||key.contains("恐")||key.contains("散")||
                        //    key.contains("其")||key.contains("全")){
                        //    for (Novel novel : novels) {
                        //        System.out.println(key.toString().trim());
                                //novel.setType(key.toString().trim());
                        //    }
                        //}else {
                            for (Novel novel : novels) {
                                novel.setFirstLetter(key.charAt(0));
                            }
                        //}

                        SqlSession session = sqlSessionFactory.openSession();
                        session.insert("batchInsert", novels);
                        session.commit();
                        session.close();

                        Thread.sleep(1_000);
                    }
                    return key;
                }
            }));
        }

        service.shutdown();

        for (Future<String> future : futures) {
            try {
                System.out.println("抓取[" + future.get() + "],结束");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
